package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gmall.common.util.Jsons;
import com.atguigu.gulimall.product.dto.ValueSkuJsonDTO;
import com.atguigu.gulimall.product.entity.SpuSaleAttr;
import com.atguigu.gulimall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gulimall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nicc
 * @description 针对表【spu_sale_attr(spu销售属性)】的数据库操作Service实现
 * @createDate 2022-09-26 18:25:42
 */
@Service
public class SpuSaleAttrServiceImpl extends ServiceImpl<SpuSaleAttrMapper, SpuSaleAttr>
        implements SpuSaleAttrService{

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {

        List<SpuSaleAttr> result = spuSaleAttrMapper.getSpuSaleAttrAndValue(spuId);
        return result;
    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrAndValueAndMarkCurrentSku(Long skuId, Long spuId) {
        return spuSaleAttrMapper.getSpuSaleAttrAndValueAndMarkCurrentSku(skuId,spuId);
    }

    @Override
    public String getSpuValuesSkuJson(Long spuId) {
        List<ValueSkuJsonDTO> dtos = spuSaleAttrMapper.getSpuValuesSkuJson(spuId);

        //System.out.println(dtos);
        // 响应式编程
        Map<String,Long> skuValues = new HashMap<>();
        dtos.stream().forEach(item->{
            skuValues.put(item.getAttrValueConcat(),item.getSkuId());
        });

        String toStr = Jsons.toStr(skuValues);
        return toStr;
    }
}